package com.jzy.bxb.manage.controller;

import java.util.List;
import java.util.Optional;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jzy.bxb.core.handler.HttpCode;
import com.jzy.bxb.manage.core.vo.ResultVO;
import com.jzy.bxb.manage.service.CommunityService;
import com.jzy.bxb.model.mongo.entity.config.ConfigCity;
import com.jzy.bxb.model.mongo.entity.config.ConfigCounty;

/**
 * 小区
 * 
 * @author JiangZhiYong QQ:359135103
 *
 */
@Controller
@RequestMapping("/community")
public class CommunityController {
	private static final Logger LOGGER = LoggerFactory.getLogger(CommunityController.class);

	@Autowired
	private CommunityService communityService;

	/**
	 * 小区列表
	 * 
	 * @param session
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/list")
	public ModelAndView communityList(HttpSession session, HttpServletResponse response) {
		try {
			List<String> citys = communityService.getCityNames();
			return new ModelAndView("/community", "citys", citys);
		} catch (Exception e) {
			LOGGER.error("小区", e);
			return new ModelAndView("redirect:/login");
		}
	}
	
	/**
	 * 获取一个城市信息
	 * <p>
	 * http://127.0.0.1:80/bxb/community/get?city=bj
	 * </p>
	 * 
	 * @param session
	 * @param response
	 * @param city
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/get")
	public ResultVO<ConfigCity> get(HttpSession session, HttpServletResponse response,
			@RequestParam String city) {
		ResultVO<ConfigCity> resultVO = new ResultVO<ConfigCity>(HttpCode.ERROR, "", null);
		ConfigCity configCity = communityService.getConfigCity(city);
		if (configCity == null) {
			resultVO.setMessage(String.format("城市%s不存在", city));
			return resultVO;
		}
		resultVO.setResult(configCity);
		resultVO.setStatus(HttpCode.SUCCESS);
		return resultVO;
	}

	/**
	 * 添加城市
	 * <p>
	 * http://127.0.0.1:80/bxb/community/add.city?province=bj&city=bj
	 * </p>
	 * 
	 * @param session
	 * @param response
	 * @param city
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/add.city")
	public ResultVO<ConfigCity> addCity(HttpSession session, HttpServletResponse response, String province,
			@RequestParam String city) {
		ResultVO<ConfigCity> resultVO = new ResultVO<ConfigCity>(HttpCode.ERROR, "", null);
		ConfigCity configCity = communityService.getConfigCity(city);
		if (configCity != null) {
			LOGGER.warn("{}城市{}已存在", province, city);
			resultVO.setMessage("城市已添加");
			return resultVO;
		}
		configCity = new ConfigCity();
		configCity.setProvince(province);
		configCity.setName(city);
		communityService.save(configCity);
		resultVO.setResult(configCity);
		resultVO.setStatus(HttpCode.SUCCESS);
		resultVO.setMessage(String.format("[%s]添加成功", city));
		return resultVO;
	}

	/**
	 * 添加区县
	 * <p>
	 * http://127.0.0.1:80/bxb/community/add.county?county=dq&city=bj
	 * </p>
	 * 
	 * @param session
	 * @param response
	 * @param city
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/add.county")
	public ResultVO<ConfigCity> addCounty(HttpSession session, HttpServletResponse response,
			@RequestParam String county, @RequestParam String city) {
		ResultVO<ConfigCity> resultVO = new ResultVO<ConfigCity>(HttpCode.ERROR, "", null);
		ConfigCity configCity = communityService.getConfigCity(city);
		if (configCity == null) {
			LOGGER.warn("城市{}不存在", city);
			resultVO.setMessage("城市不存在，请先添加");
			return resultVO;
		}
		boolean anyMatch = configCity.getCounty().stream().anyMatch(c -> c.getName().equals(county));
		if (anyMatch) {
			resultVO.setMessage(String.format("%s 已添加 %s", configCity.getName(), county));
			return resultVO;
		}
		configCity.getCounty().add(new ConfigCounty(county));
		communityService.save(configCity);
		resultVO.setResult(configCity);
		resultVO.setStatus(HttpCode.SUCCESS);
		resultVO.setMessage(String.format("%s添加成功", county));
		return resultVO;
	}

	/**
	 * 添加小区
	 * <p>
	 * http://127.0.0.1:80/bxb/community/add.community?county=dq&city=bj&community=ls
	 * </p>
	 * 
	 * @param session
	 * @param response
	 * @param city
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/add.community")
	public ResultVO<ConfigCity> addCommunity(HttpSession session, HttpServletResponse response,
			@RequestParam String county, @RequestParam String city, @RequestParam String community) {
		ResultVO<ConfigCity> resultVO = new ResultVO<ConfigCity>(HttpCode.ERROR, "", null);
		ConfigCity configCity = communityService.getConfigCity(city);
		if (configCity == null) {
			LOGGER.warn("城市{}不存在", city);
			resultVO.setMessage("城市不存在，请先添加");
			return resultVO;
		}

		Optional<ConfigCounty> findAny = configCity.getCounty().stream().filter(c -> c.getName().equals(county))
				.findAny();
		ConfigCounty configCounty = null;
		if (!findAny.isPresent()) {
			configCounty = new ConfigCounty(county);
			configCity.getCounty().add(configCounty);
		} else {
			configCounty = findAny.get();
		}
		boolean anyMatch = configCounty.getCommunity().stream().anyMatch(c -> c.equals(community));
		if (anyMatch) {
			resultVO.setMessage(String.format("%s %s 已添加小区 %s", configCity.getName(), configCounty.getName(), county));
			return resultVO;
		}
		configCounty.getCommunity().add(community);
		communityService.save(configCity);
		resultVO.setResult(configCity);
		resultVO.setStatus(HttpCode.SUCCESS);
		resultVO.setMessage(String.format("小区：%s添加成功", community));
		return resultVO;
	}

	/**
	 * 删除城市
	 * <p>
	 * http://127.0.0.1:80/bxb/community/add.county?county=dq&city=bj
	 * </p>
	 * 
	 * @param session
	 * @param response
	 * @param city
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete.city")
	public ResultVO<ConfigCity> deleteCity(HttpSession session, HttpServletResponse response,
			@RequestParam String city) {
		ResultVO<ConfigCity> resultVO = new ResultVO<ConfigCity>(HttpCode.SUCCESS, String.format("%s:删除成功", city), null);
		communityService.deleteById(city);
		return resultVO;
	}
	
}
